﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using YouMa.Model;
using YouMa.Reposity;

namespace YouMa.Controllers
{
    [Produces("application/json")]
    [Route("api/Message")]
    public class MessageController : BaseController
    {
        private readonly MessageReposity MessageReposity;
        public MessageController(MessageReposity _MessageReposity)
        {

            MessageReposity = _MessageReposity;
        }

        /// <summary>
        /// 首页刷新获得信息
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="pagenum"></param>
        /// <returns></returns>
        [HttpGet, Route("GetIndexMsgList")]
        public IActionResult GetIndexMsgList(int pagesize, int pagenum)
        {
            var Pagelist = MessageReposity.GetIndexMsgList(pagesize,pagenum);
            if (Pagelist != null)
            {
                return Ok(new { msg = "刷新成功", isSuccess = true });
            }
            else
            {
                return Ok(new { msg = "刷新失败", isSuccess = false });
            }
        }

        /// <summary>
        /// 用户发布一条message
        /// </summary>
        /// <param name="uesrid"></param>
        /// <returns></returns>
        [HttpPost,Route("MessageAdd")]
        public IActionResult MessageAdd(string uesrid)
        {
            var mes = UserReposity.InsertEntity2<Message>(a => a.UserId = uesrid);
            //mes.ThemeId = entity.ThemeId;
            //mes.CreateAddress = entity.CreateAddress;
            //mes.Job = entity.Job;
                if (mes)
                {
                    return Ok(new { msg = "发布成功", isSuccess = true });
                }
                else
                {
                    return Ok(new { msg = "发布失败", isSuccess = false });
                }
        }

        /// <summary>
        /// 获取用户发表的信息列表
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        [HttpGet, Route("GetMsgList")]
        public IActionResult GetMsgList(int userid)
        {
            var list = MessageReposity.GetList<Message>(a => a.UserId == userid);
            if (list!= null&&list.Count>0)
            {
                return Ok(new { msg = "获取列表成功", isSuccess = true });
            }
            else
            {
                return Ok(new { msg = "获取列表失败", isSuccess = false });
            }
        }

        /// <summary>
        /// 获取用户收藏的信息列表
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        [HttpGet, Route("GetSCMsgList")]
        public IActionResult GetSCMsgList(int userid)
        {
            var list = MessageReposity.GetSCMsgList(userid);
            if (list != null&&list.Count > 0)
            {
                return Ok(new { msg = "获取列表成功", isSuccess = true });
            }
            else
            {
                return Ok(new { msg = "获取列表失败", isSuccess = false });
            }
        }

        /// <summary>
        /// 获取用户关注主题的所有信息
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="pagesize"></param>
        /// <param name="pagenum"></param>
        /// <returns></returns>
        [HttpGet, Route("GetGZTMes")]
        public IActionResult GetGZTMes(int userid, int pagesize, int pagenum)
        {
            var Pagelist = MessageReposity.GetGZMsgList(userid, pagesize, pagenum);
            if (Pagelist != null)
            {
                return Ok(new { msg = "刷新成功", isSuccess = true });
            }
            else
            {
                return Ok(new { msg = "刷新失败", isSuccess = false });
            }
        }

        /// <summary>
        /// 获取信息的评论列表
        /// </summary>
        /// <param name="msgid"></param>
        /// <returns></returns>
        [HttpGet, Route("GetPLMsgList")]
        public IActionResult GetPLMsgList(int msgid)
        {
            var list = MessageReposity.GetList<PingLun>(a=>a.MsgId==msgid);
            if (list != null && list.Count > 0)
            {
                return Ok(new { msg = "获取列表成功", isSuccess = true });
            }
            else
            {
                return Ok(new { msg = "获取列表失败", isSuccess = false });
            }
        }

    }
}